<?php

/**
 * 图片位置管理
 */

namespace app\panel\controller;

use app\common\controller\Panelbase;
use app\common\model\Picture as pic_model;
use app\common\model\PictureData;
use think\Request;

class Picture extends Panelbase
{
    public function initialize()
    {
        parent::initialize();
        $this->model    = new pic_model();
        $this->pic_data = new PictureData();
        $this->validate_name = 'app\common\validate\Picture';
    }

    public function index()
    {
        $count = $this->model->count('id');
        $list  = $this->model->order('id desc')->paginate(15);

        $this->assign("list", $list);
        return $this->fetch();
    }

    public function delete()
    {
        $id = input("param.id", 0, 'intval');
        if (empty($id) || !is_numeric($id)) {
            $this->error('参数错误');
        }
        $control_array = array('Group');
        $control = request::instance()->controller();
        if (in_array($control, $control_array) && $id == 1) {
            $this->error('当前数据不支持删除');
        }
        $picData_count = $this->pic_data->where(['picid'=>$id])->count('id');
        if (!empty($picData_count)) {
            $this->error('当前位置有图片数据无法删除');
        }
        $res = $this->model->where(array('id'=>$id))->delete();
        if ($res !== false) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * 对应图片位置的图片列表
     * @return mixed
     */
    public function picList()
    {
        $picId  = input('picid');
        $picRes = $this->getPicData($picId);
        if ($picRes['code'] == 1) {
            $this->error($picRes['msg']);
        }
        $picData = $picRes['data'];
        $where   = array('picid'=>$picId);

        $count = $this->pic_data->where($where)->count('id');
        $list  = $this->pic_data->where($where)->order('sort desc, id desc')->paginate(25);

        $this->assign('pic', $picData);
        $this->assign("list", $list);
        return $this->fetch();
    }

    /**
     * 添加图片
     * @return mixed
     */
    public function picAdd()
    {
        $picId  = input('picid');
        $picRes = $this->getPicData($picId);
        if ($picRes['code'] == 1) {
            $this->error($picRes['msg']);
        }
        $picData = $picRes['data'];

        $this->assign('pic', $picData);
        return $this->fetch();
    }

    public function picAdd_post()
    {
        if (request()->isPost()) {
            $data = input('post.');

            // 数据验证
            $result = $this->validate($data, 'pictureData');
            if (true !== $result) {
                $this->error($result);
            }

            if ($this->pic_data->save($data)) {
                $this->success('保存成功', Url('picList', array('picid'=>$data['picid'])));
            } else {
                $this->error($this->pic_data->getError());
            }
        } else {
            $this->error('请求错误');
        }
    }

    public function picEdit()
    {
        $picId = input('picid');
        $id    = input("param.id");

        $picRes = $this->getPicData($picId);
        if ($picRes['code'] == 1) {
            $this->error($picRes['msg']);
        }
        $picData = $picRes['data'];

        $row = $this->pic_data->where('id', $id)->find();
        if (empty($row)) {
            $this->error('数据不存在');
        }
        $row = $row->toArray();

        $this->assign($row);
        $this->assign('pic', $picData);
        return $this->fetch();
    }

    public function picEdit_post()
    {
        if (request()->isPost()) {
            $data = input('post.');
            // 数据验证
            $result = $this->validate($data, 'pictureData');
            if (true !== $result) {
                $this->error($result);
            }

            if ($this->pic_data->isUpdate(true)->save($data)) {
                $this->success('保存成功', Url('picList', array('picid'=>$data['picid'])));
            } else {
                $this->error($this->pic_data->getError());
            }
        } else {
            $this->error('请求错误');
        }
    }

    public function picDel()
    {
        $id = input("param.id", 0, 'intval');
        if (empty($id) || !is_numeric($id)) {
            $this->error('参数错误');
        }

        $res = $this->pic_data->where(array('id'=>$id))->delete();
        if ($res !== false) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    public function picSort()
    {
        $post = input("post.");
        if (empty($post['sort']) || !is_array($post['sort'])) {
            $this->error('数据不能为空');
        }
        $data = array();
        foreach ($post['sort'] as $key=>$val) {
            $data[$key]['id']   = $key;
            $data[$key]['sort'] = $val;
        }
        $res = $this->pic_data->saveAll($data);
        if ($res !== false) {
            $this->success('排序成功');
        } else {
            $this->error('排序失败');
        }
    }

    /**
     * 判断图片位置id信息
     * @author baiyongjun
     * @date   18-01-20
     * @param  int $picid   图片位置id
     * @return array
     */
    private function getPicData($picid = 0)
    {
        $where = array('id'=>$picid);
        $find  = $this->model->where($where)->find();
        if (empty($find)) {
            return array('code'=>1, 'msg'=>'图片位置id错误');
        }
        $find = $find->toArray();
        return array('code'=>0, 'data'=>$find);
    }
}
